﻿using System.Web;
using System.Web.Configuration;

namespace SAP.BusinessOne.Web.Proxy
{
    public class QuickForwardingModule : IHttpModule
    {
        private static readonly string Host = WebConfigurationManager.AppSettings["RedirectHost"];

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            // request.LogonUserIdentity will only be available after Authenticate is called
            context.PostAuthenticateRequest += (sender, args) =>
            {
                var app = sender as HttpApplication;
                if (app != null)
                {
                    // forcibily cease further request
                    app.CompleteRequest();

                    // get domain user alias
                    var request = app.Context.Request;
                    var response = app.Context.Response;
                    var user = request.LogonUserIdentity.Name;

                    // forward
                    HttpBot.QuickForward(request, response, Host, user);
                }
            };
        }
    }
}
